﻿var app = angular.module('sovigazApp', ['ui.bootstrap']);

app.run(function ($rootScope) {
    $rootScope.opened = [{}];
    $rootScope.open = function ($event, name) {
        $event.preventDefault();
        $event.stopPropagation();
   
        if ($rootScope.opened[name] == true)
            $rootScope.opened[name] = false;
        else
            $rootScope.opened[name] = true;
    };
});

app.factory('service', function () {
    return {
        setSelected: function ($scope, id) {
            if ($scope.checkedList.indexOf(id) === -1)
                $scope.checkedList.push(id);
            else
                $scope.checkedList.pop(id);

            $scope.checkedObj[id] = !$scope.checkedObj[id];
        },
        getSelected: function ($scope) {
            var selectedObj = '';
            for (var i = 0; i < $scope.checkedList.length; i++) {
                selectedObj = selectedObj + ',' + $scope.checkedList[i];
            }

            return selectedObj;
        },
        getLastSelected: function ($scope, $compile, service) {
            if ($scope.checkedList.length === 0) {
                service.showModal($scope, $compile, 'error', 'Bạn phải chọn 1 dòng dữ liệu.');
                return '';
            } else
                return $scope.checkedList[$scope.checkedList.length - 1];
        },
        setSelectedAll: function ($scope) {
            var i;
            if ($scope.selectedAll) {
                $scope.selectedAll = true;
                for (i = 0; i < $scope.items.length; i++) {
                    if ($scope.checkedList.indexOf($scope.items[i][$scope.keyName]) === -1) {
                        $scope.checkedList.push($scope.items[i][$scope.keyName]);
                    }
                    $scope.checkedObj[$scope.items[i][$scope.keyName]] = true;
                }
            } else {
                $scope.selectedAll = false;
                for (i = 0; i < $scope.items.length; i++) {
                    if ($scope.checkedList.indexOf($scope.items[i][$scope.keyName]) !== -1) {
                        $scope.checkedList.pop($scope.items[i][$scope.keyName]);
                    }
                    $scope.checkedObj[$scope.items[i][$scope.keyName]] = false;
                }
            }
        },
        setCommonData: function ($scope, data, items) {
            $scope.items = items;
            $scope.keyName = data.KeyName;
            $scope.totalItemCount = data.TotalItemCount;
            $scope.totalItemOnPage = data.TotalItemOnPage;
            $scope.pageCount = data.PageCount;
            $scope.pageNumber = data.PageNumber;
            $scope.fromEntry = data.TotalItemOnPage === 0 ? 0 : data.PageNumber * data.PageSize - data.PageSize + 1;
            $scope.toEntry = data.PageNumber * data.PageSize - data.PageSize + data.TotalItemOnPage;
            $scope.pageList = data.PageList;
            $scope.prevDisabled = data.PageNumber <= 3 ? 'disabled' : '';
            $scope.nextDisabled = data.PageNumber >= data.PageCount - 2 ? 'disabled' : '';
        },
        setSortingValue: function ($scope, ob) {
            if (ob !== $scope.orderBy) {
                $scope.orderBy = ob;
                $scope.orderDirection = 'ascending';
            } else {
                $scope.orderDirection = $scope.orderDirection === 'ascending' ? 'descending' : 'ascending';
            }
        },
        showModal: function ($scope, $compile, which, msg, func) {
            switch (which) {
                case 'error':
                    $scope.error = msg;
                    $('#modal-error').modal({ show: true });
                    $('body').css('padding-right', '0');
                    break;
                    //case 'wait':
                    //    dlg = $scope.wait(msgs[i++], progress);
                    //    fakeProgress();
                    //    break;
                case 'notify':
                    $scope.notify = msg;
                    $('#modal-notify').fadeIn();
                    $('#modal-notify').trigger('notify');
                    break;
                case 'confirm':
                    $scope.confirm = msg;
                    $('#btn-modal-yes').replaceWith($('#btn-modal-yes'));
                    $('#btn-modal-yes').attr('ng-click', func);
                    $compile($('#btn-modal-yes'))($scope);
                    $('#modal-confirm').modal({ show: true });
                    $('body').css('padding-right', '0');
                    break;

                    //    // Create Your Own Dialog
                    //case 'create':
                    //    dlg = $scope.create('/dialogs/whatsyourname.html', 'whatsYourNameCtrl', {}, { key: false, back: 'static' });
                    //    dlg.result.then(function (name) {
                    //        $scope.name = name;
                    //    }, function () {
                    //        $scope.name = 'You decided not to enter in your name, that makes me sad.';
                    //    });

                    //    break;
            };
        }
    };
});

$(function () {
    //$('body').css('overflow', 'hidden');
    $('#side-menu').metisMenu();
    $(window).bind("load resize", function () {
        var topOffset = 50;
        var width = (this.window.innerWidth > 0) ? this.window.innerWidth : this.screen.width;
        if (width < 768) {
            $('div.navbar-collapse').addClass('collapse');
            topOffset = 100; // 2-row-menu
        } else {
            $('div.navbar-collapse').removeClass('collapse');
        }

        var height = (this.window.innerHeight > 0) ? this.window.innerHeight : this.screen.height;
        height = height - topOffset;
        if (height < 1) height = 1;
        if (height > topOffset) {
            $("#page-wrapper").css("min-height", (height - 2) + "px");
        }
    });

    $('#modal-notify').on('notify', function () {
        var notification = $(this);
        clearTimeout(notification.data('hideInterval'));
        notification.data('hideInterval', setTimeout(function () {
            notification.fadeOut();
        }, 4000));
    });

    $('.div-select').click(function () {
        $(this).parent('div.custom-select').children('div.options').toggle();
    });
});


function startloading(element) {
    var loading = $('#loading');
    var offset = $(element).offset();

    loading.width($(element).width());
    loading.height($(element).height());
    loading.offset().left = offset.left;
    loading.offset().top = offset.top;
    loading.css('display', 'table-cell');

    $(element).css('opacity', '0.3');
}

function stoploading(element) {
    $('#loading').fadeOut('fast');
    $(element).css('opacity', '1');
}